package com.bnyy.medicalHousekeeper.moudle.message.adapter;

import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.GradientDrawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.core.content.ContextCompat;
import androidx.recyclerview.widget.RecyclerView;

import com.blankj.utilcode.util.SizeUtils;
import com.bnyy.medicalHousekeeper.R;
import com.bnyy.medicalHousekeeper.moudle.message.MessageHeaderHelper;
import com.bnyy.medicalHousekeeper.moudle.message.activity.ChatMessageActivity;
import com.bnyy.message.bean.ContactGroup;
import com.bnyy.message.enums.GroupChatIdentity;

import java.util.ArrayList;
import java.util.function.Consumer;

/**
 * Author : 关炳鹏
 * Description :
 * Date : 2022/12/13
 **/
public class MyGroupChatAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private Consumer<ContactGroup> consumer;
    private Context context;
    private LayoutInflater inflater;
    private ArrayList<Object> datas = new ArrayList<>();

    public MyGroupChatAdapter(Context context, Consumer<ContactGroup> consumer) {
        this.consumer = consumer;
        this.context = context;
        inflater = LayoutInflater.from(context);
    }

    public void refresh(ArrayList<Object> datas) {
        this.datas.clear();
        if (datas != null && datas.size() > 0) {
            this.datas.addAll(datas);
        }
        notifyDataSetChanged();
    }

    @Override
    public int getItemViewType(int position) {
        Object o = datas.get(position);
        if (o instanceof String) {
            return 0;
        }
        return 1;
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        if (viewType == 0) {
            int padding = SizeUtils.dp2px(12);

            LinearLayout linearLayout = new LinearLayout(context);
            linearLayout.setOrientation(LinearLayout.VERTICAL);
            linearLayout.setPadding(0, padding, 0, 0);

            TextView tv = new TextView(context);
            tv.setId(TypeViewHolder.TV_TYPE_ID);
            tv.setTextAppearance(R.style.NormalTextView);
            tv.getPaint().setFakeBoldText(true);
            tv.setPadding(padding, padding, padding, padding);
            tv.setBackgroundColor(Color.WHITE);

            linearLayout.addView(tv);
            inflater.inflate(R.layout.divider, linearLayout, true);
            return new TypeViewHolder(linearLayout);
        }
        return new GroupChatViewHolder(inflater.inflate(R.layout.item_message_my_group_chat, parent, false));
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        Object o = datas.get(position);
        if (holder instanceof TypeViewHolder) {
            TypeViewHolder typeViewHolder = (TypeViewHolder) holder;
            typeViewHolder.tvType.setText(String.valueOf(o));
        } else {
            GroupChatViewHolder groupChatViewHolder = (GroupChatViewHolder) holder;
            ContactGroup contactGroup = (ContactGroup) o;

            MessageHeaderHelper.setHeader(context, groupChatViewHolder.rlHeaders, contactGroup.getImgs());
            groupChatViewHolder.tvName.setText(contactGroup.getGroup_name());

            GroupChatIdentity groupChatIdentity = contactGroup.getGroupChatIdentity();
            int type = contactGroup.getAuth_type();
            int groupType = contactGroup.getGroup_type();

            int green = ContextCompat.getColor(context, R.color.green_theme);

            GradientDrawable bg = (GradientDrawable) groupChatViewHolder.tvOperate.getBackground();
            if (bg == null) {
                bg = new GradientDrawable();
                bg.setCornerRadius(360);
                bg.setStroke(1, green);
            }
            bg.setColor(Color.WHITE);
            groupChatViewHolder.tvOperate.setTextColor(green);
            if (groupType == 1) { // 群聊类型 1:家庭 2普通
                groupChatViewHolder.tvOperate.setText("与家人聊天");
            } else {
                switch (groupChatIdentity) {
                    case MEMBER:
                        groupChatViewHolder.tvOperate.setVisibility(View.GONE);
                        break;
                    case MANAGER:
                    case GROUP_LEADER:
                        groupChatViewHolder.tvOperate.setVisibility(View.VISIBLE);

                        if (contactGroup.isIs_open()) {
                            groupChatViewHolder.tvOperate.setText("取消公开");
                        } else {
                            groupChatViewHolder.tvOperate.setText("公开");
                            groupChatViewHolder.tvOperate.setTextColor(Color.WHITE);
                            bg.setColor(green);
                        }
                        break;
                }
            }
            groupChatViewHolder.tvOperate.setBackground(bg);

            groupChatViewHolder.tvOperate.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    consumer.accept(contactGroup);
                }
            });

            holder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    ChatMessageActivity.show(context, contactGroup);
                }
            });
        }
    }

    @Override
    public int getItemCount() {
        return datas.size();
    }

    public static class GroupChatViewHolder extends RecyclerView.ViewHolder {

        RelativeLayout rlHeaders;
        TextView tvName;
        TextView tvOperate;

        public GroupChatViewHolder(@NonNull View itemView) {
            super(itemView);
            rlHeaders = itemView.findViewById(R.id.rl_headers);
            tvName = itemView.findViewById(R.id.tv_name);
            tvOperate = itemView.findViewById(R.id.tv_operate);
        }
    }

    public static class TypeViewHolder extends RecyclerView.ViewHolder {

        public static final int TV_TYPE_ID = 88;

        TextView tvType;

        public TypeViewHolder(@NonNull View itemView) {
            super(itemView);

            tvType = itemView.findViewById(TV_TYPE_ID);
        }
    }
}
